/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/
/********************************************************************\
                            Includes
\********************************************************************/
#include <QDebug>
#include "htrsresource.h"

/********************************************************************\
                            Static
\********************************************************************/
qint32 HtrsResource::sNextId = 1;

/********************************************************************\
                           Implementation
\********************************************************************/
/*--------------------------------------------------------------------
*/
HtrsResource::HtrsResource(const QString &url, Htr::ResourceType type)
{
  mUrl  = url;
  mType = type;
  mId   = sNextId++; 
}

/*--------------------------------------------------------------------
*/
HtrsResource::~HtrsResource()
{
}

/*--------------------------------------------------------------------
*/
void  HtrsResource::setUrl(const QString &url)
{
  mUrl = url;
}

/*--------------------------------------------------------------------
*/
QString HtrsResource::url() const
{
  return mUrl;
}

Htr::ResourceType HtrsResource::type() const
{
  return mType;
}

/*--------------------------------------------------------------------
*/
qint32 HtrsResource::id() const
{
  return mId;
}

/*--------------------------------------------------------------------
*/
void  HtrsResource::cbMsg(qint32 id, HtrManagedSocket *sock)
{
  if (mId == id)
    handleMsg(sock);
}

/*--------------------------------------------------------------------
*/
void  HtrsResource::sendError(Htr::Res::Error errCode, HtrManagedSocket *sock)
{
  Q_ASSERT(sock);

  QDataStream &outStream = sock->prepareStream();
  outStream << (qint32)Htr::Net::ClResourceMsg;
  outStream << id();
  outStream << (qint32)Htr::Net::ErrorMsg;
  outStream << (qint32)errCode;
  sock->writeStream();
}
